package dao.admin.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import dao.BaseDao;
import dao.admin.BorrowListDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


/**
 * BorrowListDaoImpl
 *
 * @author lenovo
 * @since 2023/9/5
 */
public class BorrowListDaoImpl extends BaseDao implements BorrowListDao {
    @Override
    public JSONArray getList(String condition, String conditionValue, int pageIndex, int pageSize) {
        String sql = "select id, card_id, book_id,DATE_FORMAT(borrow_date, '%Y-%m-%d %k:%i:%s') as borrow_date," +
                "DATE_FORMAT(return_date, '%Y-%m-%d %k:%i:%s') as return_date," +
                "DATE_FORMAT(end_date, '%Y-%m-%d %k:%i:%s') as end_date," +
                "illegal, manager_id from borrow_books";
        if (condition!=null && conditionValue != null && !condition.equals("") && !conditionValue.equals("")){
            sql += " where "+ condition +" like '%" +conditionValue +"%' ";
        }
        sql += " limit ?,?";
        JSONArray json = new JSONArray();
        JSONObject list = new JSONObject();
        Connection conn = getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1,pageIndex);
            pstmt.setInt(2,pageSize);
            rs = pstmt.executeQuery();
            while (rs.next()){
                list.put("id", rs.getString("id"));
                list.put("card_id", rs.getString("card_id"));
                list.put("book_id", rs.getString("book_id"));
                list.put("borrow_date", rs.getString("borrow_date"));
                list.put("end_date", rs.getString("end_date"));
                list.put("return_date", rs.getString("return_date"));
                list.put("illegal", rs.getString("illegal"));
                list.put("manager_id", rs.getString("manager_id"));
                json.add(list);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeAll(rs,pstmt,conn);
        }
        return json;
    }

    @Override
    public int total(String condition, String conditionValue) {
        String sql = "select COUNT(*) c from borrow_books ";
        if (condition!=null && conditionValue != null && !condition.equals("") && !conditionValue.equals("")){
            sql += " where "+ condition +" like '%" +conditionValue +"%' ";
        }
        int total = 0;
        Connection conn = getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            if (rs.next()){
                total = rs.getInt("c");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeAll(rs,pstmt,conn);
        }
        return total;
    }
}
